package com.mine.frame.common.net.utils

import android.util.Log
import com.blankj.utilcode.util.LogUtils
import com.mine.frame.common.BuildConfig
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.logging.HttpLoggingInterceptor


/**
 * company 广东玛格家居有限公司
 * @author smoker
 * @Date 2021/11/11
 * Description:日志拦截器
 */
class InterceptorUtil {
    companion object {
        private const val TAG = "InterceptorUtil"

        /**
         * 日志拦截器,用于打印返回请求的结果
         */
        fun logInterceptor(): HttpLoggingInterceptor {
            val mLog =  HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
                if (message.startsWith("{") || message.startsWith("[")) {
                    Log.w(TAG, "json:$message")
                    LogUtils.json(message)
                } else {
                    LogUtils.i(message)
                }
            })
            if (BuildConfig.DEBUG) {
                mLog.level = HttpLoggingInterceptor.Level.BODY
            } else {
                mLog.level = HttpLoggingInterceptor.Level.NONE
            }
            return mLog
        }


        /**
         * 头部添加请求头信息
         */
        fun headerInterceptor(): Interceptor {
            return Interceptor { chain ->
                val request: Request = chain.request().newBuilder()
                    .addHeader("Content-Type", "application/json;charSet=UTF-8")
                    .build()
                chain.proceed(request)
            }
        }
    }

}